package com.plugin.sproutcore.core.resources.properties;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

/**
 * Holds details about a SproutCore project.
 */
@XStreamAlias(value="project")
public class SproutCoreProject 
{
	private String name;
	private String path;
	
	@XStreamAsAttribute
	private String lastAppTarget;
	
	public SproutCoreProject() {}
	
	/**
	 * Name of the project
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	
	/**
	 * Relative path (from project path) to the app
	 * @param path
	 */
	public void setPath(String path) {
		this.path = path;
	}
	public String getPath() {
		return path;
	}
	
	/**
	 * Get a list of all the SproutCore apps in a project.
	 * 
	 * @param project
	 * The project where the SproutCore project is located.
	 * 
	 * @return List of all the SproutCore apps.
	 * @throws CoreException 
	 */
	public IContainer[] getApps(IProject project) throws CoreException 
	{
		List<IContainer> appList = new ArrayList<IContainer>();
		IPath path = Path.fromPortableString(getPath());
		IContainer sproutCoreProjectFile = (IContainer)project.findMember(path);
		
		if (sproutCoreProjectFile != null) 
		{
			IContainer appFolder = (IContainer)sproutCoreProjectFile.findMember("apps");
			if (appFolder != null) 
			{
				IResource[] appFileList = appFolder.members();
				for (IResource appFile:appFileList)
				{
					if (appFile instanceof IContainer) 
					{
						appList.add((IContainer)appFile);
					}
				}
			}
		}
		return appList.toArray(new IContainer[0]);
	}

	public void setLastAppTarget(String lastAppTarget) {
		this.lastAppTarget = lastAppTarget;
	}
	public String getLastAppTarget() {
		return lastAppTarget;
	}

}
